#!/usr/bin/env python

# 键盘控制器补丁，用于替换gym_hil库中的pynput键盘控制器

import sys
import importlib
from pathlib import Path

# 导入我们自定义的pygame键盘控制器
sys.path.append(str(Path(__file__).parent.parent.parent))
from common.controllers.pygame_keyboard_controller import PygameKeyboardController

# 导入gym_hil库中的intervention_utils模块
import gym_hil.wrappers.intervention_utils as intervention_utils

# 保存原始的KeyboardController类，以便需要时恢复
original_keyboard_controller = intervention_utils.KeyboardController

def patch_keyboard_controller():
    """
    使用我们的PygameKeyboardController替换原始的KeyboardController
    """
    print("正在应用键盘控制器补丁，使用基于pygame的键盘控制器...")
    intervention_utils.KeyboardController = PygameKeyboardController
    print("键盘控制器补丁应用成功！")
    
def restore_keyboard_controller():
    """
    恢复原始的KeyboardController
    """
    intervention_utils.KeyboardController = original_keyboard_controller
    print("已恢复原始键盘控制器")

if __name__ == "__main__":
    # 测试补丁
    patch_keyboard_controller()
    
    # 创建一个新的控制器实例
    controller = intervention_utils.KeyboardController(0.01, 0.01, 0.01)
    print(f"创建的控制器类型: {type(controller)}")
    
    # 恢复原始控制器
    restore_keyboard_controller()
